<template>
  <div class="todo-footer">
    <label>
      <!-- <input type="checkbox" :checked="xxx" @change="xxx" /> -->
      <input type="checkbox" v-model="checkAll" />
    </label>
    <span>
      <span>已完成 {{ todoStore.isDoneNum }}</span> / 全部
      {{ todoStore.totalNum }}
    </span>
    <!-- 表达式不长，就不用计算属性优化了 -->
    <button
      class="btn btn-danger"
      v-show="todoStore.isDoneNum > 0"
      @click="removeAllTodo"
    >
      清除已完成任务
    </button>
  </div>
</template>

<script lang="ts">
export default {
  name: "Footer",
};
</script>
<script lang="ts" setup>
import { computed } from "vue";
import useTodoStore from "../../store/modules/todo";

const todoStore = useTodoStore();

const checkAll = computed({
  get() {
    // 1. 决定checkbox是选中还是不选中
    // 看是否全选，全选返回true，不是返回false
    return (
      todoStore.isDoneNum === todoStore.totalNum && todoStore.totalNum !== 0
    );
  },
  set(val: boolean) {
    // console.log(val);
    todoStore.updateCheckAllTodo(val);
  },
});

const removeAllTodo = () => {
  todoStore.removeAllTodo();
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
